feat: implement bulk delete and undelete#18
Conversation
|
Thank you @EastSun5566 ! It looks like there is a subtle bug in Line 576 in c646a57 I've pushed a unit test demonstrating the issue. Since deleting unknown ids should be rare, it's okay to skip the fast path in this case and fall back on calling deleteOne. The other thing I wonder is: how hard would it be to also optimize the case where the deleted ids are spread across several leaf nodes? Something like: bulk-delete as many as you can in the first leaf, then if you're not done, call |
|
Oh, you're right! First, I implemented a quick fix if first id to delete is not known should fall back to deleting one by one: if (located !== null) {
// bulk delete
}
// fall back to delete one by one
Yeah, that makes total sense. Here's my approach: I track while (remaining > 0) {
const located = ans.locate(currentId);
if (located === null) {
// skip and move to next
continue;
}
// calculate how many ids can be deleted in this leaf
if (canDeleteInLeaf) {
// bulk delete and move to next
} else {
// fallback: delete one by one
}
}I also added two simple tests that demonstrate bulk |
src/id_list.ts
Outdated
| if ( | ||
| leaf.bunchId === currentId.bunchId && | ||
| currentId.counter >= leaf.startCounter && | ||
| canDeleteInLeaf > 0 | ||
| ) { |
There was a problem hiding this comment.
This condition should always be true, since the locate result always contains currentId. So you can actually remove the else clause and deleteOne entirely (nice!).
There was a problem hiding this comment.
Fix it! I remove the whole if statement.
src/id_list.ts
Outdated
| if ( | ||
| leaf.bunchId === currentId.bunchId && | ||
| currentId.counter >= leaf.startCounter && | ||
| canUndeleteInLeaf > 0 | ||
| ) { |
There was a problem hiding this comment.
Same as for delete (& undeleteOne can be deleted).
src/id_list.ts
Outdated
| let remaining = count; | ||
| let currentId = id; |
There was a problem hiding this comment.
I would prefer if instead of these two variables, the loop just uses a currentCounter variable that holds currentId's counter. Compare that to id.counter + count in the loop condition.
Your code works as well, this is just a style preference.
There was a problem hiding this comment.
Yes! This is much cleaner. I refactored it to use the currentCounter check condition.
Hi @mweidner037
Thanks for the awesome blog post. I'm trying to read the source and learn by contributing to it.
This PR closes #14 - it optimizes bulk
delete/undeleteoperations when ids are in the same leaf node. I also added a simple actions to run tests on every PR.If there are any problems, please let me know!